﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ProjectEulerSolutions
{
    class Problem33
    {
        public static string Calculate()
        {
            long productNominator = 1;
            long productDenominator = 1;
            for (int denominator = 10; denominator < 100; denominator++)
            {
                for (int nominator = 10; nominator < denominator; nominator++)
                {
                    if ((nominator % 10 == 0) && (denominator % 10 == 0))
                        continue;

                    var nominatorDigits = CommonFunctions.GetDigits(nominator);
                    var denominatorDigits = CommonFunctions.GetDigits(denominator);

                    var common = nominatorDigits.Intersect(denominatorDigits).ToList();
                    if (common.Count != 1)
                        continue;

                    nominatorDigits.Remove(common[0]);
                    denominatorDigits.Remove(common[0]);

                    double nominatorFactor = nominator / (double)nominatorDigits[0];
                    double denominatorFactor = denominator / (double)denominatorDigits[0];

                    if (nominatorFactor == denominatorFactor)
                    {
                        Console.WriteLine("{0}/{1} = {2}/{3}", nominator, denominator, nominatorDigits[0], denominatorDigits[0]);
                        productNominator *= nominator;
                        productDenominator *= denominator;
                    }
                }
            }

            long gcd2 = CommonFunctions.GreatestCommonDivisor(productDenominator, productNominator);
            productDenominator /= gcd2;

            return productDenominator.ToString();
        }
    }
}
